package co.edu.usbcali.dataaccess.dao;

import co.edu.usbcali.dataaccess.entityManager.EntityManagerHelper;
import co.edu.usbcali.modelo.Preinscripcion;

import java.math.BigDecimal;

import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;

import javax.persistence.EntityManager;
import javax.persistence.Query;


/**
* A data access object (DAO) providing persistence and search support for
* Preinscripcion entities. Transaction control of the save(), update() and delete()
* operations must be handled externally by senders of these methods or must be
* manually added to each of these methods for data to be persisted to the JPA
* datastore.
*
* @author Zathura Code Generator http://code.google.com/p/zathura
* @see lidis.Preinscripcion
*
*/
public class PreinscripcionDAO implements IPreinscripcionDAO {
    private EntityManager getEntityManager() {
        return EntityManagerHelper.getEntityManager();
    }

    /**
    * Perform an initial save of a previously unsaved Preinscripcion entity. All
    * subsequent persist actions of this entity should use the #update()
    * method. This operation must be performed within the a database
    * transaction context for the entity's data to be permanently saved to the
    * persistence store, i.e., database. This method uses the
    * {@link javax.persistence.EntityManager#persist(Object) EntityManager#persist}
    * operation.
    *
    * <pre>
    * EntityManagerHelper.beginTransaction();
    * PreinscripcionDAO.save(entity);
    * EntityManagerHelper.commit();
    * </pre>
    *
    * @param entity
    *            Preinscripcion entity to persist
    * @throws RuntimeException
    *             when the operation fails
    */
    public void save(Preinscripcion entity) {
        EntityManagerHelper.log("saving Preinscripcion instance", Level.INFO,
            null);

        try {
            getEntityManager().persist(entity);
            EntityManagerHelper.log("save successful", Level.INFO, null);
        } catch (RuntimeException re) {
            EntityManagerHelper.log("save failed", Level.SEVERE, re);
            throw re;
        }
    }

    /**
    * Delete a persistent Preinscripcion entity. This operation must be performed
    * within the a database transaction context for the entity's data to be
    * permanently deleted from the persistence store, i.e., database. This
    * method uses the
    * {@link javax.persistence.EntityManager#remove(Object) EntityManager#delete}
    * operation.
    *
    * <pre>
    * EntityManagerHelper.beginTransaction();
    * PreinscripcionDAO.delete(entity);
    * EntityManagerHelper.commit();
    * entity = null;
    * </pre>
    *
    * @param entity
    *            Preinscripcion entity to delete
    * @throws RuntimeException
    *             when the operation fails
    */
    public void delete(Preinscripcion entity) {
        EntityManagerHelper.log("deleting Preinscripcion instance", Level.INFO,
            null);

        try {
            entity = getEntityManager()
                         .getReference(Preinscripcion.class, entity.getCodPre());
            getEntityManager().remove(entity);
            EntityManagerHelper.log("delete successful", Level.INFO, null);
        } catch (RuntimeException re) {
            EntityManagerHelper.log("delete failed", Level.SEVERE, re);
            throw re;
        }
    }

    /**
    * Persist a previously saved Preinscripcion entity and return it or a copy of it
    * to the sender. A copy of the Preinscripcion entity parameter is returned when
    * the JPA persistence mechanism has not previously been tracking the
    * updated entity. This operation must be performed within the a database
    * transaction context for the entity's data to be permanently saved to the
    * persistence store, i.e., database. This method uses the
    * {@link javax.persistence.EntityManager#merge(Object) EntityManager#merge}
    * operation.
    *
    * <pre>
    * EntityManagerHelper.beginTransaction();
    * entity = PreinscripcionDAO.update(entity);
    * EntityManagerHelper.commit();
    * </pre>
    *
    * @param entity
    *            Preinscripcion entity to update
    * @return Preinscripcion the persisted Preinscripcion entity instance, may not be the
    *         same
    * @throws RuntimeException
    *             if the operation fails
    */
    public Preinscripcion update(Preinscripcion entity) {
        EntityManagerHelper.log("updating Preinscripcion instance", Level.INFO,
            null);

        try {
            Preinscripcion result = getEntityManager()
                                        .merge(entity);
            EntityManagerHelper.log("update successful", Level.INFO, null);

            return result;
        } catch (RuntimeException re) {
            EntityManagerHelper.log("update failed", Level.SEVERE, re);
            throw re;
        }
    }

    public Preinscripcion findById(Long id) {
        EntityManagerHelper.log("finding Preinscripcion instance with id: " +
            id, Level.INFO, null);

        try {
            Preinscripcion instance = getEntityManager()
                                          .find(Preinscripcion.class, id);

            return instance;
        } catch (RuntimeException re) {
            EntityManagerHelper.log("find failed", Level.SEVERE, re);
            throw re;
        }
    }

    /**
    * Find all  Preinscripcion entities with a specific property value.
    *
    * @param propertyName
    *            the metaData.name of the  Preinscripcion property to query
    * @param value
    *            the property value to match
    * @return List< Preinscripcion> found by query
        */
    @SuppressWarnings("unchecked")
    public List<Preinscripcion> findByProperty(String propertyName,
        final Object value) {
        EntityManagerHelper.log(
            "finding  Preinscripcion instance with property: " + propertyName +
            ", value: " + value, Level.INFO, null);

        try {
            final String queryString = "select model from  Preinscripcion model where model." +
                propertyName + "= :propertyValue";
            Query query = getEntityManager().createQuery(queryString);
            query.setParameter("propertyValue", value);

            return query.getResultList();
        } catch (RuntimeException re) {
            EntityManagerHelper.log("find by property metaData.name failed",
                Level.SEVERE, re);
            throw re;
        }
    }

    /**
    * Find all Preinscripcion entities with a specific property value.
    *
    * @param propertyName
    *            the name of the Preinscripcion property to query
    * @param value
    *            the property value to match
    * @param rowStartIdxAndCount
    *            Optional int varargs. rowStartIdxAndCount[0] specifies the the
    *            row index in the query result-set to begin collecting the
    *            results. rowStartIdxAndCount[1] specifies the the maximum
    *            number of results to return.
    * @return List<Preinscripcion> found by query
        */
    @SuppressWarnings("unchecked")
    public List<Preinscripcion> findByProperty(String propertyName,
        final Object value, final int... rowStartIdxAndCount) {
        EntityManagerHelper.log(
            "finding Preinscripcion instance with property: " + propertyName +
            ", value: " + value, Level.INFO, null);

        try {
            final String queryString = "select model from Preinscripcion model where model." +
                propertyName + "= :propertyValue";
            Query query = getEntityManager().createQuery(queryString);
            query.setParameter("propertyValue", value);

            if ((rowStartIdxAndCount != null) &&
                    (rowStartIdxAndCount.length > 0)) {
                int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);

                if (rowStartIdx > 0) {
                    query.setFirstResult(rowStartIdx);
                }

                if (rowStartIdxAndCount.length > 1) {
                    int rowCount = Math.max(0, rowStartIdxAndCount[1]);

                    if (rowCount > 0) {
                        query.setMaxResults(rowCount);
                    }
                }
            }

            return query.getResultList();
        } catch (RuntimeException re) {
            EntityManagerHelper.log("find by property name failed",
                Level.SEVERE, re);
            throw re;
        }
    }

    public List<Preinscripcion> findByCargoPre(Object cargoPre,
        int... rowStartIdxAndCount) {
        return findByProperty(CARGOPRE, cargoPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByCargoPre(Object cargoPre) {
        return findByProperty(CARGOPRE, cargoPre);
    }

    public List<Preinscripcion> findByCargoTutorPre(Object cargoTutorPre,
        int... rowStartIdxAndCount) {
        return findByProperty(CARGOTUTORPRE, cargoTutorPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByCargoTutorPre(Object cargoTutorPre) {
        return findByProperty(CARGOTUTORPRE, cargoTutorPre);
    }

    public List<Preinscripcion> findByCelularTutorPre(Object celularTutorPre,
        int... rowStartIdxAndCount) {
        return findByProperty(CELULARTUTORPRE, celularTutorPre,
            rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByCelularTutorPre(Object celularTutorPre) {
        return findByProperty(CELULARTUTORPRE, celularTutorPre);
    }

    public List<Preinscripcion> findByCodPre(Object codPre,
        int... rowStartIdxAndCount) {
        return findByProperty(CODPRE, codPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByCodPre(Object codPre) {
        return findByProperty(CODPRE, codPre);
    }

    public List<Preinscripcion> findByDescripcionPre(Object descripcionPre,
        int... rowStartIdxAndCount) {
        return findByProperty(DESCRIPCIONPRE, descripcionPre,
            rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByDescripcionPre(Object descripcionPre) {
        return findByProperty(DESCRIPCIONPRE, descripcionPre);
    }

    public List<Preinscripcion> findByEstadoPre(Object estadoPre,
        int... rowStartIdxAndCount) {
        return findByProperty(ESTADOPRE, estadoPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByEstadoPre(Object estadoPre) {
        return findByProperty(ESTADOPRE, estadoPre);
    }

    public List<Preinscripcion> findByFechaFinalPre(Object fechaFinalPre,
        int... rowStartIdxAndCount) {
        return findByProperty(FECHAFINALPRE, fechaFinalPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByFechaFinalPre(Object fechaFinalPre) {
        return findByProperty(FECHAFINALPRE, fechaFinalPre);
    }

    public List<Preinscripcion> findByFechaInicioPre(Object fechaInicioPre,
        int... rowStartIdxAndCount) {
        return findByProperty(FECHAINICIOPRE, fechaInicioPre,
            rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByFechaInicioPre(Object fechaInicioPre) {
        return findByProperty(FECHAINICIOPRE, fechaInicioPre);
    }

    public List<Preinscripcion> findByFechaPre(Object fechaPre,
        int... rowStartIdxAndCount) {
        return findByProperty(FECHAPRE, fechaPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByFechaPre(Object fechaPre) {
        return findByProperty(FECHAPRE, fechaPre);
    }

    public List<Preinscripcion> findByLaboraPre(Object laboraPre,
        int... rowStartIdxAndCount) {
        return findByProperty(LABORAPRE, laboraPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByLaboraPre(Object laboraPre) {
        return findByProperty(LABORAPRE, laboraPre);
    }

    public List<Preinscripcion> findByLaboresPre(Object laboresPre,
        int... rowStartIdxAndCount) {
        return findByProperty(LABORESPRE, laboresPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByLaboresPre(Object laboresPre) {
        return findByProperty(LABORESPRE, laboresPre);
    }

    public List<Preinscripcion> findByLugarLaboraPre(Object lugarLaboraPre,
        int... rowStartIdxAndCount) {
        return findByProperty(LUGARLABORAPRE, lugarLaboraPre,
            rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByLugarLaboraPre(Object lugarLaboraPre) {
        return findByProperty(LUGARLABORAPRE, lugarLaboraPre);
    }

    public List<Preinscripcion> findByNombreTutorPre(Object nombreTutorPre,
        int... rowStartIdxAndCount) {
        return findByProperty(NOMBRETUTORPRE, nombreTutorPre,
            rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByNombreTutorPre(Object nombreTutorPre) {
        return findByProperty(NOMBRETUTORPRE, nombreTutorPre);
    }

    public List<Preinscripcion> findByObjetivoPre(Object objetivoPre,
        int... rowStartIdxAndCount) {
        return findByProperty(OBJETIVOPRE, objetivoPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByObjetivoPre(Object objetivoPre) {
        return findByProperty(OBJETIVOPRE, objetivoPre);
    }

    public List<Preinscripcion> findByTelefonoTutorPre(
        Object telefonoTutorPre, int... rowStartIdxAndCount) {
        return findByProperty(TELEFONOTUTORPRE, telefonoTutorPre,
            rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByTelefonoTutorPre(Object telefonoTutorPre) {
        return findByProperty(TELEFONOTUTORPRE, telefonoTutorPre);
    }

    public List<Preinscripcion> findByTemasPre(Object temasPre,
        int... rowStartIdxAndCount) {
        return findByProperty(TEMASPRE, temasPre, rowStartIdxAndCount);
    }

    public List<Preinscripcion> findByTemasPre(Object temasPre) {
        return findByProperty(TEMASPRE, temasPre);
    }

    /**
    * Find all Preinscripcion entities.
    *
    * @param rowStartIdxAndCount
    *            Optional int varargs. rowStartIdxAndCount[0] specifies the the
    *            row index in the query result-set to begin collecting the
    *            results. rowStartIdxAndCount[1] specifies the the maximum
    *            count of results to return.
    * @return List<Preinscripcion> all Preinscripcion entities
    */
    @SuppressWarnings("unchecked")
    public List<Preinscripcion> findAll(final int... rowStartIdxAndCount) {
        EntityManagerHelper.log("finding all Preinscripcion instances",
            Level.INFO, null);

        try {
            final String queryString = "select model from Preinscripcion model";
            Query query = getEntityManager().createQuery(queryString);

            if ((rowStartIdxAndCount != null) &&
                    (rowStartIdxAndCount.length > 0)) {
                int rowStartIdx = Math.max(0, rowStartIdxAndCount[0]);

                if (rowStartIdx > 0) {
                    query.setFirstResult(rowStartIdx);
                }

                if (rowStartIdxAndCount.length > 1) {
                    int rowCount = Math.max(0, rowStartIdxAndCount[1]);

                    if (rowCount > 0) {
                        query.setMaxResults(rowCount);
                    }
                }
            }

            return query.getResultList();
        } catch (RuntimeException re) {
            EntityManagerHelper.log("find all failed", Level.SEVERE, re);
            throw re;
        }
    }

    public List<Preinscripcion> findByCriteria(String whereCondition) {
        EntityManagerHelper.log("finding Preinscripcion " + whereCondition,
            Level.INFO, null);

        try {
            String where = ((whereCondition == null) ||
                (whereCondition.length() == 0)) ? "" : ("where " +
                whereCondition);
            final String queryString = "select model from Preinscripcion model " +
                where;
            Query query = getEntityManager().createQuery(queryString);
            List<Preinscripcion> entitiesList = query.getResultList();

            return entitiesList;
        } catch (RuntimeException re) {
            EntityManagerHelper.log("find By Criteria in Preinscripcion failed",
                Level.SEVERE, re);
            throw re;
        }
    }

    public List<Preinscripcion> findPagePreinscripcion(String sortColumnName,
        boolean sortAscending, int startRow, int maxResults) {
        EntityManagerHelper.log("finding Preinscripcion findPagePreinscripcion",
            Level.INFO, null);

        if ((sortColumnName != null) && (sortColumnName.length() > 0)) {
            try {
                String queryString = "select model from Preinscripcion model order by model." +
                    sortColumnName + " " + (sortAscending ? "asc" : "desc");

                return getEntityManager().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .getResultList();
            } catch (RuntimeException re) {
                throw re;
            }
        } else {
            try {
                String queryString = "select model from Preinscripcion model";

                return getEntityManager().createQuery(queryString)
                           .setFirstResult(startRow).setMaxResults(maxResults)
                           .getResultList();
            } catch (RuntimeException re) {
                throw re;
            }
        }
    }

    @SuppressWarnings("unchecked")
    public Long findTotalNumberPreinscripcion() {
        EntityManagerHelper.log("finding Preinscripcion count", Level.INFO, null);

        try {
            String queryString = "select count(*) from Preinscripcion model";

            return (Long) getEntityManager().createQuery(queryString)
                              .getSingleResult();
        } catch (RuntimeException re) {
            throw re;
        }
    }
}
